var e = getApp(), u = e.require("utils/util.js"), a = e.require("utils/api.js"), o = e.require("utils/onfire.js"), h = e.require("utils/http.js"), n = e.require("utils/enums");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sortName:[
      { id: 0, name: '综合', keyword: '', style: 'color:#6A98F5;' },
      { id: 1, name: '销量', keyword: 'sales', style: '' },
      { id: 2, name: '价格', keyword: 'price_asc', style: '' },
    ],
    indexs:1,
    sortIcon:'/packages/images/sort-asc.png',
    pageIndex:1,
    limit:8,
    keyword:'',
    group_id:'',
    sort:'',
    is_host:0,
    searchProductList:[],
    brandList:[],
    currentBrandIndex: '',
    minPrice: '',
    maxPrice: '',
    userInfo: null,
    couponPage:1,
    ungetCouponList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.userInfo = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo') : app.doLogin(),
    wx.setNavigationBarTitle({
      title: options.title
    }),
    this.data.group_id = options.group_id ? options.group_id : '',
    this.getSearchProduct();
  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function (e) {
    this.getSearchProduct();    
  },

  // 获取未领取优惠券列表
  ungetCouponList:function(){
    var t = this;
    var data = {
      store_id: a.storeId,
      uid: t.data.userInfo.uid,
      type:'all',
      page: t.data.couponPage
    }
    h.noLoadReq(a.userCouponList, data, 'POST', function (res) {
      if (res.code == 0) {
        var couponList = [];
        couponList.push(res.data.coupon_list[0]);
        couponList.push(res.data.coupon_list[1]);
        t.setData({
          ungetCouponList: couponList
        });
      }
    });
  },

  // 获取所有搜索药品列表
  getSearchProduct: function (){
    var t = this;
    var data = {
      store_id: a.storeId,
      keyword:t.data.keyword,
      group_id:t.data.group_id,
      sort: t.data.sort,
      page:t.data.pageIndex,
      is_host:t.data.is_host,
      limit:t.data.limit,
      brand_id: t.data.currentBrandIndex,
      price_min: t.data.minPrice,
      price_max: t.data.maxPrice
    }
    h.postReq(a.getStoreSearch,data,function(res){
      if(res.code == 0){        
        t.setData({
          searchProductList: t.data.searchProductList.concat(res.data.product_list),
          pageIndex: t.data.pageIndex + 1,
          brandList: res.data.brand_list
        });
      }else{
        u.toast(res.message);
      }
    });
  },
  // 商品关键词搜索
  search:function(e){
    var t = this;
    t.data.keyword = e.detail.value;
    t.data.searchProductList = [];
    t.data.pageIndex = 1;
    t.getSearchProduct();
  },
  // 选择排序类型
  sortClick:function(e){
    var t = this;
    var sortKeyword = e.currentTarget.dataset.sort;
    for (var i = 0; i < t.data.sortName.length; i++){
      t.data.sortName[i].style = '';
      if (sortKeyword == t.data.sortName[i].keyword){
        t.data.sortName[i].style = "color:#6A98F5;"
        t.data.sort = t.data.sortName[i].keyword;
        t.data.searchProductList = [];
        t.data.pageIndex = 1;
        t.getSearchProduct();
      }
      t.setData({
        sortIcon: '/packages/images/sort-asc.png',
        sortName: t.data.sortName
      });
    }
  },

  // 选择价格排序规则
  sortPriceClick:function(e){
    var t = this;
    var index = e.currentTarget.dataset.index;
    if (t.data.sort == 'price_asc' || t.data.sort == 'price_desc'){
      if (index == 1) {
        this.setData({
          sort: 'price_desc',
          indexs: index + 1,
          sortIcon: '/packages/images/sort-desc.png'
        })
        t.data.searchProductList = [];
        t.data.pageIndex = 1;
        t.getSearchProduct();
      } else {
        this.setData({
          sort: 'price_asc',
          indexs: index - 1,
          sortIcon: '/packages/images/sort-asc.png'
        })
        t.data.searchProductList = [];
        t.data.pageIndex = 1;
        t.getSearchProduct();
      }
    }
    
  },
  // 商品详情
  proudctInfo: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../index/product/info?id=' + id,
    })
  },
  // 加入购物车
  addCart: function (e) {
    var t = this;
    var product_id = e.currentTarget.dataset.id
    var quantity = 1;
    var data = {
      store_id: a.storeId, uid: t.data.userInfo.uid, product_id: product_id, quantity: quantity
    };
    // console.log(data);
    // 加入购物车流程     
    h.noLoadReq(a.addCart, data, 'POST', function (res) {
      if (res.code == 0) {
        u.success('成功加入购物车');
      } else {
        u.toast(res.message);
      }
    });
  },

  // 筛选部分
  showPopup: function () {
    let popupComponent = this.selectComponent('.J_Popup');
    popupComponent && popupComponent.show();
  },
  cancel: function () {
    let popupComponent = this.selectComponent('.J_Popup');
    popupComponent && popupComponent.hide();
    // console.log('取消')
    this.setData({ minPrice: null, maxPrice:null, currentBrandIndex: 0 });
  },
  formSubmit: function (e) {
    let popupComponent = this.selectComponent('.J_Popup');
    popupComponent && popupComponent.hide();
    // console.log('确定');
    var t = this;
    var price_min = e.detail.value.minPrice;
    var price_max = e.detail.value.maxPrice;
    var brand_id = t.data.currentBrandIndex ? t.data.currentBrandIndex: ''; 
    t.setData({ 
      currentBrandIndex: brand_id, 
      minPrice: price_min,
      maxPrice: price_max
    });
    t.data.searchProductList = [];
    t.data.pageIndex = 1;
    t.getSearchProduct(); 
  },
  // 品牌的选择
  brandSelect:function(e){
    var t = this;
    var brand_id = e.currentTarget.dataset.id;
    // console.log(brand_id);
    t.setData({ currentBrandIndex:brand_id});
  }

})